require([
	'jquery',
	'module/util',
	'module/popup',
	'global',
	'datatables.fixedcolumns',
	'requirejs/domready!'],
function($, util, popup, g){
	var permission_table = null,
		permission_fc_table = null;
	
	// 渲染表格
	$('#permission-table').on('click', 'tbody td', function(){
		var dt = $('#ur-table').dataTable().api();
		var idx = dt.cell(this).index();
		if(!idx) return;
		var row = dt.row(idx.row).data();
		$(this).parents('tbody').find('tr.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		var $ctr = $(this).parents('.DTFC_ScrollWrapper')
			.find('.DTFC_LeftBodyLiner .DTFC_Cloned tbody tr');
		$ctr.filter('.selected').removeClass('selected');
		$($ctr.get(idx.row)).addClass('selected');
	}).on('init.dt', function(){
		permission_fc_table = new $.fn.dataTable.FixedColumns(
    		$('#permission-table').dataTable().api(), {
				"iLeftColumns": 1,
				"iLeftWidth": 220
    		});
    }).on('draw.dt', function(){
    	if(permission_table){
    		permission_table.columns.adjust();
    	}
		if(permission_fc_table){
			permission_fc_table.fnRedrawLayout();
		}
    }).dataTable( {
    	language: {
            url: g.context + '/assets/js/chinese.json'
        },
        dom: 'rt<"row"<"col-md-4"i><"col-md-8"p>>',
    	processing : true,
	    serverSide : true,
	    scrollX: '100%',
		scrollCollapse : true,
	    ajax: {
	    	type: 'POST',
	    	url: g.context + '/admin/permissions',
	    	contentType: 'application/json; charset=utf-8',
	    	global: false,
	    	data: function(d){
	    		return JSON.stringify(d);
	    	},
	    	error: function(xhr, status, error){
	    		console.log(error);
	    	}
	    },
	    columnDefs: [ 
	     	{ targets : ['_all'], defaultContent: '&nbsp;', searchable : false }
	    ],
	    columns: [
	        { data: 'permission'},
	        { data: 'description'},
	        { data: 'createdAt'},
	        { data: 'updatedAt'}
	    ]
    });
	permission_table = $('#permission-table').dataTable().api();
});